<?php

/*
 * 任务类.

 */

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * Description of Task
 *
 * @author Administrator
 */
class Task extends Backend {

    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('Task');
    }

    /*
     * 任务列表
     *      
     */

    public function index() {

        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            $task = $this->model;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $task
                    ->where($where)
                    ->where('status=1')
                    ->order($sort, $order)
                    ->count();
            $list = $task
                    ->where($where)
                    ->where('status=1')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->fetch();
    }

    /*
     * 添加任务
     */

    public function add() {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params['subtime'] = $params['subtime1'] * 60 * 60 + $params['subtime2'] * 60;
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign('groupList', build_select('row[typeid]', \app\admin\model\Tasktype::column('id,title'), [], ['class' => 'form-control selectpicker']));
        return $this->fetch();
    }
/*
 * 编辑任务
 */
    public function edit($ids = NULL) {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params['subtime'] = $params['subtime1'] * 60 * 60 + $params['subtime2'] * 60;
            if ($params) {
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign('groupList', build_select('row[typeid]', \app\admin\model\Tasktype::column('id,title'), [], ['class' => 'form-control selectpicker']));
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /*
     * 删除任务
     */
   public function del($ids = '')
    {
       $ids = $this->request->param('ids');
        $row = $this->model->where('id',$ids)->update(['status'=>'-1']);
        if($row)
        {
             $this->success();
        }
        else
        {
             $this->error('失败请重试');
        }
 
        return $this->view->fetch();
    }
    /*
     * 财务信息
     */
    public function finance()
    {
                $this->request->filter(['strip_tags']);
                $task = $this->model;
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $task
                    ->where($where)
                    ->where('status=1')
                    ->order($sort, $order)
                    ->count();
            $list = $task
                    ->where($where)
                    ->where('status=1')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
             $list = $task
                    ->where('status>0')
                    ->select();
              $this->view->assign("list", $list);
        return $this->fetch('index');
    }

}